//
//  LYPresentationController.swift
//  Swift自定义转成动画练习
//
//  Created by liyang on 16/8/3.
//  Copyright © 2016年 liyang. All rights reserved.
//

import UIKit

class LYPresentationController: UIPresentationController {
    
    private lazy var coverView : UIView = UIView()
    
    // 所有的被modal出来的控制器都是被添加在一个containerView上的，通过重写这个方法，来改变modal出控制器frame
    override func containerViewWillLayoutSubviews() {
        // 1、设置frame
        presentedView()?.frame = CGRect(x: 50, y: 55, width: 180, height: 250)
        // 2、设置蒙版
        setupCoverView()
    }
}
// MARK: - 设置蒙版
extension LYPresentationController {
    private func setupCoverView() {
        // 1、添加蒙版
        containerView?.insertSubview(coverView, atIndex: 0)
        coverView.frame = containerView!.bounds
        // 2、给蒙版添加一个手势，当点击蒙版的时候，是控制器dismiss
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        let tapGr = UITapGestureRecognizer(target: self, action: #selector(LYPresentationController.coverViewClick))
        coverView.addGestureRecognizer(tapGr)
    }
}

// MARK: - 控件的监听方法
extension LYPresentationController {
    @objc private func coverViewClick() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
